/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.smn.jsf;

import com.smn.dao.TicketFacade;
import com.smn.domain.MyUser;
import com.smn.domain.Ticket;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;

/**
 *
 * @author Abdulaziz
 */
@ManagedBean(name = "myTicketController")
@SessionScoped
public class MyTicketController {

    private Ticket currentTicket;

    public Ticket getCurrentTicket() {
        return currentTicket;
    }

    public void setCurrentTicket(Ticket currentTicket) {
        this.currentTicket = currentTicket;
    }
    
    @EJB
    private TicketFacade ticketFacade;
    
    @ManagedProperty(value = "#{myUserController}")
    private MyUserController myUserController;

    @ManagedProperty(value = "#{shareController}")
    private ShareController shareController;

    public MyUserController getMyUserController() {
        return myUserController;
    }

    public ShareController getShareController() {
        return shareController;
    }

    public void setShareController(ShareController shareController) {
        this.shareController = shareController;
    }
    
    public void setMyUserController(MyUserController myUserController) {
        this.myUserController = myUserController;
    }

    public TicketFacade getTicketFacade() {
        return ticketFacade;
    }

    public void setTicketFacade(TicketFacade ticketFacade) {
        this.ticketFacade = ticketFacade;
    }

    
    
    /** Creates a new instance of MyTicketController */
    public MyTicketController() {
        
//        S
//        FacesContext context = FacesContext.getCurrentInstance();
//        context.get
//        Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
//        flash.get("")
    }
    public String ticketID(){
        FacesContext fc = FacesContext.getCurrentInstance();
        String ticketId = fc.getExternalContext().getRequestParameterMap().get("ticketid");
        //Ticket ticket = ticketFacade.find(ticketId);
        return ticketId;
    }
    public MyUser getUser(){
     Ticket current = myTicket();
     setCurrentTicket(current);
     return myUserController.getCurrent();
    }
    public Ticket myTicket(){
        FacesContext fc = FacesContext.getCurrentInstance();
        String ticketId = fc.getExternalContext().getRequestParameterMap().get("ticketid");
        Long idLong = Long.valueOf(ticketId);
        Ticket ticket = ticketFacade.find(idLong);
        myUserController.setCurrentUser(ticket.getReferer());
        shareController.setCurrent(ticket.getShare());
        return ticket;
    }
    
    public boolean isAttending(){
        return currentTicket.isAccepted();
    }
    public String goAhead(){
        ticketFacade.edit(currentTicket);
        return "spread";
    }
}
